﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PraxisRent.Entities;
using PraxisRent.BL;
using Microsoft.Practices.Unity;

namespace PraxisRent.UI
{
    public partial class FrmCliente : Form
    {
        [Dependency]
        public ClienteBL clienteBL { get; set; }

        [Dependency]
        public ITelefonoClienteBL teleClienteBL { get; set; }

        [Dependency]
        public IClienteJuridicoBL cliJurBL { get; set; }

        [Dependency]
        public IClienteNaturalBL cliNatBL { get; set; }

        [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        public FrmCliente()
        {
            InitializeComponent();
            this.BuilUp();
        }

        bool isExtend;

        public void Extend()
        {
            isExtend = true;
        }

        public USUARIO currentUsuarioCliente { get; set; }

        private void FrmCliente_Load(object sender, EventArgs e)
        {
            if (currentUsuarioCliente != null)
            {
                menuLlena();
            }
            else
                cargaData();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioCliente.PERSONA.Dni);

            var permisos = (from c in usuMenuBL.GetAllFromUsuarioMenuByMenuId(datoUsuario.UsuarioId)
                            select c.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[] { 
            this.btnNuevo,this.btnEditar};

            //obtengo todos los PERMISOS NO ASIGNADOS al usuario
            var noVisible = from nv in botones
                            where !permisos.Contains(nv.Tag.ToString())
                            select nv;

            //coloco DESABILITADO a los permisos no asignados
            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            cargaData();
        }

        public void cargaData()
        {
            cLIENTE_NATURALBindingSource.DataSource = cliNatBL.GetAllFromClienteNatural();
            cLIENTE_JURIDICOBindingSource.DataSource = cliJurBL.GetAllFromClienteJuridico();
        }

        //boton buscar cliente JURIDICO
        private void btnBuscar_Click(object sender, EventArgs e)
        {
            cLIENTE_JURIDICOBindingSource.DataSource = cliJurBL.GetAllFromClienteJuridicoByCriterio(TxtCriterioJuridico.Text);
            if (cLIENTE_JURIDICOBindingSource.Count == 0)
            {
                MessageBox.Show("Cliente no existente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        //boton buscar cliente NATURAL
        private void btnBuscarNatural_Click(object sender, EventArgs e)
        {
            cLIENTE_NATURALBindingSource.DataSource = cliNatBL.GetAllFromClienteNaturalByCriterio(txtCriterioNatural.Text);
            if (cLIENTE_NATURALBindingSource.Count == 0)
            {
                MessageBox.Show("Cliente no existente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        //evento buscar cliente JURIDICO
        private void TxtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cLIENTE_JURIDICOBindingSource.DataSource = cliJurBL.GetAllFromClienteJuridicoByCriterio(TxtCriterioJuridico.Text);
                if (cLIENTE_JURIDICOBindingSource.Count == 0)
                {
                    MessageBox.Show("Cliente no existente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        //evento buscar cliente NATURAL
        private void txtCriterioNatural_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                cLIENTE_NATURALBindingSource.DataSource = cliNatBL.GetAllFromClienteNaturalByCriterio(txtCriterioNatural.Text);
                if (cLIENTE_NATURALBindingSource.Count == 0)
                {
                    MessageBox.Show("Cliente no existente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoCliente frm = new FrmMttoCliente();
            frm.NuevoCliente();
            frm.ShowDialog();
            cargaData();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var clienteJuridico = cLIENTE_JURIDICOBindingSource.Current as CLIENTE_JURIDICO;
            var clienteNatural = cLIENTE_NATURALBindingSource.Current as CLIENTE_NATURAL;

            if (tabControl1.SelectedTab == tabJuridico)
            {
                if (clienteJuridico != null)
                {
                    FrmMttoCliente frm = new FrmMttoCliente();
                    frm._clienteJuridico = clienteJuridico;
                    frm.EditarCliente();
                    frm.ShowDialog();
                    cargaData();
                }
                else
                {
                    MessageBox.Show("Debe seleccionar un Cliente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (tabControl1.SelectedTab == tabNatural)
            {
                if (clienteNatural != null)
                {              
                    FrmMttoCliente frm = new FrmMttoCliente();
                    frm._clienreNatural = clienteNatural;
                    frm.EditarCliente();
                    frm.ShowDialog();
                    cargaData();
                }
                else
                {
                    MessageBox.Show("Debe seleccionar un Cliente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        //evento validacion TXTCRITERIO JURIDICO
        private void TxtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rbtnRazSocial.Checked == true)
            {
                var amberson = Convert.ToString("&");
                var par1 = Convert.ToString("(");
                var par2 = Convert.ToString(")");
                var punto = Convert.ToString(".");
                var guion = Convert.ToString("-");
                var coma = Convert.ToString(",");


                string cadena = amberson + par1 + par2 + punto + guion + coma;

                if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            else if (rbtnRuc.Checked == true)
            {
                if (Char.IsNumber(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        //evento validacion TXTCRITERIO NATURAL
        private void txtCriterioNatural_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rbtnPersona.Checked == true)
            {
                if (Char.IsLetter(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            else if (rbtnRuc.Checked == true)
            {
                if (Char.IsNumber(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        //validacion de los RBTN JURIDICO
        private void rbtnNomRazSocial_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnRuc.Checked == true)
            {
                TxtCriterioJuridico.Text = "";
            }
            else if (rbtnRazSocial.Checked == true)
            {
                TxtCriterioJuridico.Text = "";
            }
        }

        //validacion de los RBTN NATURAL
        private void rbtnPersona_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnPersona.Checked == true)
            {
                txtCriterioNatural.Text = "";
            }
            else if (rbtnDni.Checked == true)
            {
                txtCriterioNatural.Text = "";
            }
        }

        public CLIENTE_JURIDICO currentClienteJuridico { get; set; }

        //evento cerrar grilla cliente JURIDICO
        private void cLIENTERadGridView_DoubleClick(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
                currentClienteJuridico = cLIENTE_JURIDICOBindingSource.Current as CLIENTE_JURIDICO;
                this.Close();
            }
        }

        private void cLIENTE_JURIDICORadGridView1_CurrentCellChanged(object sender, Telerik.WinControls.UI.CurrentCellChangedEventArgs e)
        {
            try
            {
                var currentClienteJuridico = cLIENTE_JURIDICOBindingSource.Current as CLIENTE_JURIDICO;
                for (int count = 0; count < cLIENTE_JURIDICORadGridView1.Rows.Count; count++)
                {
                    if (currentClienteJuridico != null)
                    {
                        CLIENTE a = cLIENTE_JURIDICORadGridView1.Rows[count].Cells[2].Value as CLIENTE;

                        var und = (from c in cliJurBL.GetAllFromClienteJuridico()
                                   where c.ClienteJuridicoId.Equals(a.Codigo)
                                   select c).SingleOrDefault();
                        und.CLIENTE = a;
                        cLIENTE_JURIDICORadGridView1.Rows[count].Cells[4].Value = und.CLIENTE.Direccion.ToString();
                        cLIENTE_JURIDICORadGridView1.Rows[count].Cells[5].Value = und.CLIENTE.Email.ToString();
                        cLIENTE_JURIDICORadGridView1.Rows[count].Cells[6].Value = und.CLIENTE.Fax.ToString();
                        cLIENTE_JURIDICORadGridView1.Rows[count].Cells[7].Value = und.CLIENTE.Contacto.ToString();
                        cLIENTE_JURIDICORadGridView1.Rows[count].Cells[8].Value = und.CLIENTE.CelularContacto.ToString();
                        cLIENTE_JURIDICORadGridView1.Rows[count].Cells[9].Value = und.CLIENTE.Recomendable.ToString();
                        comentarioTextBox.Text = und.CLIENTE.Comentario.ToString();

                        tELEFONOCLIENTEsBindingSource.DataSource = teleClienteBL.GetAllFromTelefonoClienteByClienteJuridico(currentClienteJuridico.Ruc);
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        private void cLIENTE_NATURALRadGridView_CurrentCellChanged(object sender, Telerik.WinControls.UI.CurrentCellChangedEventArgs e)
        {
            try
            {
                var currentClienteNatural = cLIENTE_NATURALBindingSource.Current as CLIENTE_NATURAL;
                for (int count = 0; count < cLIENTE_NATURALRadGridView.Rows.Count; count++)
                {
                    if (currentClienteNatural != null)
                    {
                        CLIENTE a = cLIENTE_NATURALRadGridView.Rows[count].Cells[2].Value as CLIENTE;

                        var und = (from c in cliNatBL.GetAllFromClienteNatural()
                                   where c.ClienteNaturalId.Equals(a.Codigo)
                                   select c).SingleOrDefault();
                        und.CLIENTE = a;
                        cLIENTE_NATURALRadGridView.Rows[count].Cells[4].Value = und.CLIENTE.Direccion.ToString();
                        cLIENTE_NATURALRadGridView.Rows[count].Cells[5].Value = und.CLIENTE.Email.ToString();
                        if (und.CLIENTE.Fax != null)
                        {
                            cLIENTE_NATURALRadGridView.Rows[count].Cells[6].Value = und.CLIENTE.Fax.ToString();
                        }
                        else
                        {
                            cLIENTE_NATURALRadGridView.Rows[count].Cells[6].Value = "-";
                        }
                        cLIENTE_NATURALRadGridView.Rows[count].Cells[7].Value = und.CLIENTE.Contacto.ToString();
                        cLIENTE_NATURALRadGridView.Rows[count].Cells[8].Value = und.CLIENTE.CelularContacto.ToString();
                        cLIENTE_NATURALRadGridView.Rows[count].Cells[9].Value = und.CLIENTE.Recomendable.ToString();
                        comentarioTextBox.Text = und.CLIENTE.Comentario.ToString();

                        tELEFONOCLIENTEsBindingSource.DataSource = teleClienteBL.GetAllFromTelefonoClienteByClienteNatural(currentClienteNatural.Dni);
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }

            
        }

        public CLIENTE_NATURAL currentClienteNtural { get; set; }

        private void cLIENTE_NATURALRadGridView_DoubleClick(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
                currentClienteNtural = cLIENTE_NATURALBindingSource.Current as CLIENTE_NATURAL;
                this.Close();
            }
        }
    }
}
